import interpreter.Interpreter;
import lexer.Lexer;
import parser.AST;
import parser.Parser;
import process.OutputProcesser;
import process.Preprocesser;

import java.util.ArrayList;
import java.util.Scanner;

/**
 * @author IDo!
 * @version 1.0
 */
public class main {
    public static void main(String[] args) {

        System.out.println("Welcome to the world of lambda calculus" +
                "\nFunctional programming is fascinating" +
                "\nEnter \"quit\" to exit" +
                "\n-----------------------------------------------------------------");

        //主循环体
        Preprocesser.initialize();
        boolean inloop = true;
        //运行模式：0为解释器模式：interpreter
        //        1为纯lambda运算形式，lambda
        //        2为查看组合子表达式形式, combinator
        int run_mode = 0;
        Scanner scanner = new Scanner(System.in);

        while(inloop){
            System.out.print(">>>");
            String exp = scanner.nextLine();
            if(exp.equalsIgnoreCase("quit")){
                inloop = false;
                System.out.println("QUIT SUCCESS");
                continue;
            }

            int mode = 0;
            if ((mode = Preprocesser.isSet(exp)) >= 0){
                run_mode = mode;
                System.out.println("SET SUCCESS");
                continue;
            }

            switch (run_mode){
                case 0:
                    exp = Preprocesser.preprocess(exp);
                    if(!exp.equals("***")) {
                        Lexer lexer = new Lexer(exp);
                        Parser parser = new Parser(lexer);
                        AST ast = parser._Set();
                        Interpreter interpreter = new Interpreter();
                        AST newast = interpreter.cal(ast);
//                System.out.println(newast);
                        String line = newast.finalToString(new ArrayList<String>());
                        line = OutputProcesser.outputProcess(line);
                        System.out.println(line);
                    }else {
                        System.out.println("DEFINE SUCCESS");
                    }
                    break;
                case 1:
                    exp = Preprocesser.preprocess(exp);
                    if(!exp.equals("***")) {
                        Lexer lexer = new Lexer(exp);
                        Parser parser = new Parser(lexer);
                        AST ast = parser._Set();
                        Interpreter interpreter = new Interpreter();
                        AST newast = interpreter.cal(ast);
                        String line = newast.finalToString(new ArrayList<String>());
                        System.out.println(line);
                    }else {
                        System.out.println("DEFINE SUCCESS");
                    }
                    break;
                case 2:
                    System.out.println(exp + " : " + Preprocesser.keywordToLambda(exp));
                    break;
            }
        }
    }
}
